var app = getApp();
import {
    ajaxPost
} from '../../utils/util'
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        selectShow: {
            type: Boolean,
            value: true
        },
        addressType: {
            type: Number,
            value: 3
        },
        addressConfirmFun: {
            type: String,
            value: ''
        },
        selectVal: {
            type: Array,
            value: [{
                code: 0,
                fullName: '北京',
            }, {
                code: 0,
                fullName: '北京',
            }, {
                code: 0,
                fullName: '北京',
            }]
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        addressConfirmFun: '',
        selectShow: true,
        selectVal: [{
            value: 0,
            name: '北京',
        }, {
            value: 0,
            name: '北京',
        }, {
            value: 0,
            name: '北京',
        }],
        rollVal: [0, 0, 0],
        provinceSource: [],
        citySource: [],
        districtSource: [],
    },
    /**
     * 组件生命周期函数 - 在组件布局完成后执行
     */
    ready: function () {
        console.log('this.data.addressType', this.data.addressType);
        this.initAddress()
        return;

    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 滑动事件
        bindChange(e) {
            const changeVal = e.detail.value
            let {
                rollVal,
                provinceSource,
                citySource,
            } = this.data
            //滑动的省级
            if (changeVal[0] !== rollVal[0]) {
                let parId = provinceSource[changeVal[0]].code
                this.setData({
                    rollVal: [changeVal[0], 0, 0]
                })
                this.getcity(parId);
            }
            //滑动的市级
            if (changeVal[0] == rollVal[0] && changeVal[1] !== rollVal[1]) {
                let cityId = citySource[changeVal[1]].code
                this.setData({
                    rollVal: [changeVal[0], changeVal[1], 0]
                })
                if (this.data.addressType > 2) {
                    this.getcounty(cityId);
                }
            }
            //滑动的区级
            if (changeVal[0] == rollVal[0] && changeVal[1] == rollVal[1] && changeVal[2] !== rollVal[2]) {
                this.setData({
                    rollVal: changeVal
                })
            }
            return;
        },
        /**
         * 省市区初始化
         */
        initAddress() { // 地址选择
            console.log(this.data.selectVal);
            this.getprovince()
        },
        /**
         * 获取省级列表
         */
        getprovince() {
            console.log('addressType', this.data.addressType);
            let data = {
                code: 0
            }
            ajaxPost('/index/getopenarea', data, province => {
                console.log(province.result)
                const firstProvince = province.result[0];
                /**
                 * 默认选择获取的省份第一个省份数据
                 */
                this.setData({
                    provinceSource: province.result,
                });
                if (this.data.addressType > 1) {
                    this.getcity(firstProvince.code);
                }
            })
        },
        /**
         * 获取市级列表
         * @param {*} id  传省级id
         */
        getcity(id) {
            ajaxPost('/index/getopenarea', {
                code: id,
            }, city => {
                this.setData({
                    citySource: city.result,
                });
                if (this.data.addressType > 2) {
                    this.getcounty(city.result[0].region_id);
                }
            })
        },
        /**
         * 获取区/县列表
         * @param {*} id 传 市级id
         */
        getcounty(id) {
            ajaxPost('getcounty', {
                city: id,
            }, district => {
                this.setData({
                    districtSource: district.data,
                });
            })
        },
        /**
         *  取消
         */
        addressCancel() {
            this.setData({
                selectShow: false
            })
        },
        /**
         *  确认
         */
        addressConfirm() {
            let {
                rollVal,
                provinceSource,
                citySource,
                districtSource,
                addressConfirmFun,
            } = this.data
            let addressVal = [{
                fullName: provinceSource[rollVal[0]].fullName,
                code: provinceSource[rollVal[0]].code,
            }]
            if (this.data.addressType > 1) {
                addressVal.push({
                    fullName: citySource[rollVal[1]].fullName,
                    code: citySource[rollVal[1]].code,
                })
            }
            if (this.data.addressType > 2) {
                addressVal.push({
                    fullName: districtSource[rollVal[2]].fullName,
                    code: districtSource[rollVal[2]].code,
                })
            }
            this.triggerEvent(addressConfirmFun, {
                selectVal: addressVal,
            });
            this.setData({
                selectShow: false
            })
        }
    }
});